import tkinter
from tkinter import ttk
from tkinter.filedialog import askdirectory
import sys
import subprocess

win = tkinter.Tk()
win.title('faster-whisper-gui')
win.geometry('350x300')

list_model_size = ['tiny.en', 'tiny', 'base.en', 'base', 'small.en', 'small',
                   'medium.en', 'medium', 'large-v1', 'large-v2', 'large-v3']
list_language = ['zh', 'en', 'ja', 'None']
list_device = ['cuda', 'cpu']
list_compute_type = ['float32', 'float16', 'int8_float16', 'int8']


def create_combobox(window, label_text, combobox_value,
                    combobox_current_value, combobox_id):
    tkinter.Label(window, text=label_text).grid(row=combobox_id, column=0)

    com = ttk.Combobox(win)
    com.grid(row=combobox_id, column=1)
    com['value'] = combobox_value
    com.current(combobox_current_value)
    return com


info = [['model_size', list_model_size, 10],
        ['language', list_language, 3],
        ['device', list_device, 0],
        ['compute_type', list_compute_type, 1]]
component_list = []
sum = None
for j, i in enumerate(info):
    component_list.append((i[0], create_combobox(win, i[0], i[1], i[2], j)))
    sum = j

info = ['input_path', 'output_path']


class PathSelector():
    def __init__(self, label_text, row_id):
        self.path = tkinter.StringVar()
        tkinter.Label(win, text=label_text).grid(row=row_id, column=0)
        self.entry = tkinter.Entry(win, textvariable=self.path)
        self.entry.grid(row=row_id, column=1)
        tkinter.Button(win, text='select_path', command=self.select_path) \
            .grid(row=row_id, column=2)

    def select_path(self):
        path_ = askdirectory()
        if path_ == '':
            self.path.get()
        else:
            path_ = path_.replace('/', '\\')
            self.path.set(path_)

    def get(self):
        return self.entry.get()


count = None
for j, i in enumerate(info):
    component_list.append((i, PathSelector(i, sum + j + 1)))
    count = sum + j + 1
sum = count


def start():
    command = '%s faster-whisper.py' % (sys.executable)
    for i in component_list:
        value = i[1].get()
        if value == '' or value == 'None':
            continue
        command += ' --%s %s' % (i[0], value)
    print(command)
    subprocess.run(command, capture_output=False, check=True)
    print('finished')


btn = tkinter.Button(win, text='start', command=start, width=20)
btn.grid(row=sum + 1, column=0, columnspan=3)

# fm = tkinter.Frame(win)
# fm.grid(rowspan=6, columnspan=3, sticky=tkinter.W + tkinter.E + tkinter.N + tkinter.S)
#
# tkinter.Label(fm, text='terminal').pack()
#
# s2 = tkinter.Scrollbar(fm)
# b2 = tkinter.Scrollbar(fm, orient='horizontal')
# s2.pack(side='right', fill='y')
# b2.pack(side='bottom', fill='x')
#
# text = tkinter.Text(fm, undo=True, autoseparators=False, wrap='none',
#                     xscrollcommand=b2.set, yscrollcommand=s2.set)
# text.pack(fill='both')
# s2.config(command=text.yview)
# b2.config(command=text.xview)

win.mainloop()
